﻿using System;
using System.Collections.Generic;
using Color = System.Drawing.Color;
using SwinGame;

namespace Gundam_2._0_Alpha.src
{
    public class Ability
    {
        private bool isActive = false, bossAbility = false;
        protected Timer recastTimer;
        private Bitmap icon;
        private KeyCode k;
        protected Character c;
        protected CharacterAction abilityAction;
        private string name;
        private int duration;

        public void SetIcon(string iconPath)
        {
            icon = Resources.LoadBitmap(iconPath);
        }

        public bool BossAbility
        {
            get { return bossAbility; }
            set { bossAbility = value; }
        }

        public bool IsActive
        {
            get { return isActive; }
            set { isActive = value; }
        }
        
        public string Name
        {
            get { return name; }
        }

        public KeyCode K
        {
            get { return k; }
        }
        
        public CharacterAction AbilityAction
        {
            get { return abilityAction; }
            set { abilityAction = value; }
        }

        public Ability(Character c, KeyCode k, string name, int duration)
        {
            this.c = c;
            this.k = k;
            this.name = name;
            this.duration = duration;
            if (duration != 0)
                recastTimer = Core.CreateTimer();
        }

        public virtual void Update(GameController gc)
        {
            if (!isActive && !c.GlobalLockout && !c.LockoutTiers[abilityAction.LockoutLevel])
            {
                if (duration != 0)
                    Core.StartTimer(recastTimer);
                abilityAction.DoAction(gc);
                isActive = true;
            }
            
            if (abilityAction.Persistant && abilityAction.IsActive)
                abilityAction.DoAction(gc);

            if (duration == 0)
            {
                isActive = false;
                return;
            }
            if (Core.TimerTicks(recastTimer) > duration)
                Reset();
        }

        public void Draw()
        {
            if (isActive && abilityAction is CharDrawAction && abilityAction.IsActive)
                (abilityAction as CharDrawAction).Draw();
        }

        public void Draw(int xPos, int yPos)
        {
            Draw();

            if (!(c is Player)) return;
            icon.Draw(xPos, yPos);
            string[] splitString = name.Split(' ');

            Color col;
            if (isActive || c.GlobalLockout) col = Color.Gray;
            else col = Color.Lime;

            Text.DrawText(splitString[0], col, (float)xPos, (float)yPos + 40);
            if (splitString.Length > 1)
                Text.DrawText(splitString[1], col, (float)xPos, (float)yPos + 55);
            Text.DrawText("["+k.ToString().Remove(0, 3)+"]", col, (float)xPos, (float)yPos + 70);
        }

        public virtual void Reset()
        {
            if (duration != 0)
                Core.StopTimer(recastTimer);
            isActive = false;
        }

        public void Free()
        {
            if (duration != 0)
                Core.FreeTimer(recastTimer);
            icon.DoFree();
        }
    }
}
